package ils.web.action;

import com.opensymphony.xwork2.Preparable;
import ils.domain.model.User;
import ils.services.AdminService;

import java.util.Map;

import ils.util.SpringUtil;
import ils.util.Util;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

public class AddUserAction extends ActionSupport implements SessionAware, Preparable
{
    private Map<String, Object> session;
    private User user;
    private AdminService adminService;
    public String execute() throws Exception
    {
        if(user == null) return INPUT;
        user.setPasswordHash(Util.md5(user.getPasswordHash()));
        adminService.addUser(user);
        return SUCCESS;
    }
    @Override
    public void setSession(Map<String, Object> arg0)
    {
        this.session = arg0;
    }
    public User getUser()
    {
        return user;
    }
    public void setUser(User user)
    {
        this.user = user;
    }
    public void setAdminService(AdminService adminService)
    {
        this.adminService = adminService;
    }

    @Override
    public void prepare() throws Exception {
        adminService = (AdminService) SpringUtil.getApplicationContext().getBean("adminService");
    }
}
